package com.hakgc.rent.web.servlet;

import com.alibaba.fastjson.JSON;
import com.hakgc.rent.dto.Result;
import com.hakgc.rent.service.DistrictService;
import com.hakgc.rent.service.impl.DistrictServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Xing.Jiejian
 * @date 2020/11/9
 */
@WebServlet(name = "DelDistrictServlet",urlPatterns = "/rent/district/del")
public class DelDistrictServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取编号
        String strId = request.getParameter("id");
        // 将提示信息使用JavaScript显示出来
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        //构建响应结果
        Result result = new Result();
        result.setCode(-1);
        if (strId!=null) {
            int id = Integer.parseInt(strId);
            DistrictService districtService = new DistrictServiceImpl();
            // 响应执行结果
            if (districtService.deleteById(id)) {
                result.setCode(1);
            }else{
                result.setErrorMsg("删除失败，请重新再试！");
            }
        }else{
            result.setErrorMsg("没有指定的区县编号");
        }
        out.println(JSON.toJSONString(result));

    }
}
